/*
 * @Author: 阿芙
 * @Date: 2021-06-14 23:56:54
 * @LastEditTime: 2021-07-01 18:13:04
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: /RuoLing-SQL/test/test.js
 */

//express模块
var express = require('express');

const {createPool} = require("../Index.js")




var app = express();
let config = {
    host:"192.168.50.128",
    user:"root",
    password:"lvdi123",
    database:"ruoling_test",
    connectionLimit : 10,
}
const db = createPool(config);
//console.log(db)
app.get("/test",async function(req,res){
    let startTrans = null;
    try  {
        const connect = await db.connect();
        
        startTrans = await connect.startTrans();
        //小明转给小红 300
       // let str = await startTrans.query("UPDATE sys_qian SET money = money+300 where name = '小红'");
          let str = await startTrans.query("insert into sys_qian(name,money)values('小君',1000)");
        //小明转给小红 300
       // throw new Error("错误了")
       // let str1 = await startTrans.query("UPDATE sys_qian SET money = money-300 where name = '小明'");
        startTrans.commit();
        res.send({"data":1});
        return;
   } catch (error) {
  
    startTrans.rollback();
    res.send({"data":"错误"});
   }


   
    
})

app.get("/test1",async function(req,res){
        let connect = null;
    try {
         connect = await db.connect();

        throw new Error("醋味了")
        let str = "";
        
        let data = await connect.connect_query_close("select * from sys_user where id = 111102");

        res.send({"data":data})
    } catch (error) {
        connect.close();
        console.log(error)
        res.send({"error":error});
        return;
    }


   
    
})



app.listen(3001,()=>{
    console.log(3000+"端口开启成功")
})